package cn.xu.bookstore.order.web.servlet;



import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.xu.bookstore.cart.domain.Cart;
import cn.xu.bookstore.cart.domain.CartItem;
import cn.xu.bookstore.order.domain.Order;
import cn.xu.bookstore.order.domain.OrderItem;
import cn.xu.bookstore.order.excepion.OrderException;
import cn.xu.bookstore.order.service.OrderService;
import cn.xu.bookstore.user.domain.User;

public class OrderServlet extends BaseServlet {
	
	private OrderService orderService = new OrderService();
	
	public String add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		/*
		 * 一个购物车对应一个订单
		 * 购物车的东西放到订单中后清空购物车
		 * 
		 */
		Cart cart =(Cart)request.getSession().getAttribute("cart");
		User user = (User)request.getSession().getAttribute("session_user");
		/*
		 * 创建订单
		 */
		Order order = new Order();
		order.setOid(CommonUtils.uuid());
		order.setState(1);//设置状态为1标识为付款
		order.setOrdertime(new Date());
		order.setTotal(cart.getTotal());
		order.setOwner(user);
		/*
		 * 创建订单条目集合
		 */
		List<OrderItem> orderItems = new ArrayList<OrderItem>();
		for(CartItem cartItem : cart.getCartItems()){
			OrderItem orderItem = new OrderItem();
			orderItem.setBook(cartItem.getBook());
			orderItem.setCount(cartItem.getCount());
			orderItem.setSubtotal(cartItem.getSubtotal());
			orderItem.setIid(CommonUtils.uuid());
			orderItem.setOrder(order);
			//添加该订单条目
			orderItems.add(orderItem);
		}
		
		order.setOrderItems(orderItems);
		//清空购物车
		cart.clear();
		orderService.add(order);
		request.setAttribute("order", order);
		//该订单已经生成则跳转到购买的页面
		return "f:/jsps/order/desc.jsp";
	}
	
	public String myOrders(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 *查询我的订单
		 *从session中获取 该用户
		 *根据该用户 得到该用户的订单
		 *然后保存结果到request域中 ，请求转发到list.jsp
		 */
		User user = (User)request.getSession().getAttribute("session_user");
		List<Order> orders = orderService.myOrders(user.getUid());
		
		request.setAttribute("orders", orders);
		
		
		return "f:/jsps/order/list.jsp";
	}
	
	
	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 根据页面的oid
		 * 加载指定订单
		 * 详情完成付款
		 */
		String oid = request.getParameter("oid");
		Order order = orderService.load(oid);
		request.setAttribute("order", order);
		return "f:jsps/order/desc.jsp";
	}
	
	/*
	 * 确认收货
	 */
	public String confirm(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String oid = request.getParameter("oid");
		try {
			orderService.confirm(oid);
			request.setAttribute("msg", "交易成功");
		} catch (OrderException e) {
			request.setAttribute("msg", e.getMessage());
		}
		
		return "f:/jsps/msg.jsp";
	}
	
	
	

}
